<?php
use Qiniu\Auth as QiniuAuth;
use Qiniu\Storage\UploadManager;

if (! function_exists('uploads'))
{
function uploads($file,$image)
{
    $accessKey = 'Npg7Sanmf4z8uv3mvwwffjOvoCMYN8Ezm4T8pDrC';
    $secretKey = '_vSWntsyKFYRn2AoP4FWhSCQjIHxMLWp_6e3OQsC';
    // 构建鉴权对象
    $auth = new QiniuAuth($accessKey, $secretKey);
    // 要上传的空间
    $bucket = 'anwendai';
    // 生成上传 Token
    $token = $auth->uploadToken($bucket);
    // 要上传文件的本地路径
//        $filePath = 'E:/xampp/htdocs/temp/public/uploads/gs/149543806142873.jpg';
//        $filePath = getRealPath();
    $filePath = $_FILES[$image]['tmp_name'];
    // 上传到七牛后保存的文件名
    $date = time().mt_rand(10000,99999);
    $key = $date.'.'.$file->getClientOriginalExtension();
    // 初始化 UploadManager 对象并进行文件的上传。
    $uploadMgr = new UploadManager();
    // 调用 UploadManager 的 putFile 方法进行文件的上传。
    //上传home_image
    list($ret, $err1) = $uploadMgr->putFile($token, $key, $filePath);
    if ($err1 !== null) {
        return redirect()->back()->with(['info'=>'图片上传失败']);
    }

    $baseUrl1 = 'http://or2eh71ll.bkt.clouddn.com/'.$key;
    $authUrl1 = $auth->privateDownloadUrl($baseUrl1);
        return $authUrl1;
}
}
